package com.code1030.utils;

import com.code1030.studentsystem.Student;

import java.util.ArrayList;

public class ArrayUtils {
    //私有化构造方法 为了不让外界创建它的对象
    private ArrayUtils() {
        System.out.println('1');
    }

    //打印数组方法 打印格式[1,2,3]
    public static String printArray(int[] arr) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]);
            if (i < arr.length - 1) {
                sb.append(",");
            }
        }
        sb.append("]");

        return sb.toString();
    }

    //返回平均值 （只考虑double类型数组）
    public static double getAverage(double[] arr) {
        double sum = 0.0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        sum = sum / arr.length;
        return sum;
    }

    //返回最大年龄学生对象
    public static Student getAverageObj(ArrayList<Student> list) {
        Student stu = null;
        int maxAge = 0;
        for (int i = 0; i < list.size(); i++) {
           Student stuItem = list.get(i);
            if (stuItem.getAge() > maxAge) {
                maxAge = stuItem.getAge();
                stu=stuItem;
            }
        }
        return stu;
    }
}
